﻿using System;
using LaunchPad.Core.Maths;
using System.ComponentModel;

namespace LaunchPad.Core
{
    public class ProductCharacterizationSample : INotifyPropertyChanged
    {
        public ProductCharacterizationSample() :
            this(DateTime.Now)
        {
        }

        public ProductCharacterizationSample(DateTime recordedOn) :
            this(recordedOn, TimeSpan.Zero)
        {
        }

        public ProductCharacterizationSample(DateTime recordedOn, TimeSpan apex) :
            this(recordedOn, apex, Distance.Zero)
        {
        }

        public ProductCharacterizationSample(DateTime recordedOn, TimeSpan apex, Distance distance)
        {
            RecordedOn = recordedOn;
            Apex = apex;
            Distance = distance;
        }

        private DateTime _recordedOn;
        private TimeSpan _apex;
        private Distance _distance;

        public DateTime RecordedOn
        {
            get { return _recordedOn; }
            set
            {
                if (_recordedOn != value)
                {
                    _recordedOn = value;
                    OnPropertyChanged("RecordedOn");
                }
            }
        }

        public TimeSpan Apex
        {
            get { return _apex; }
            set
            {
                if (_apex != value)
                {
                    _apex = value;
                    OnPropertyChanged("Apex");
                    OnPropertyChanged("AdjustedApex");
                }
            }
        }

        public Distance Distance
        {
            get { return _distance; }
            set
            {
                if (_distance != value)
                {
                    _distance = value;
                    OnPropertyChanged("Distance");
                    OnPropertyChanged("AdjustedApex");
                }
            }
        }

        public TimeSpan AdjustedApex
        {
            get 
            { 
                var adjustedApex = Apex - TimeSpan.FromSeconds(Distance.SoundSeconds);
                if (adjustedApex < TimeSpan.Zero)
                    return TimeSpan.Zero;
                return adjustedApex;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
